import qs from 'qs'
import axios from 'axios'
import {errorMsgDialog, warnMsgDialog} from '@/util/common'
import {Spin} from 'ant-design-vue'
import VueDB from './vue-db'
import Router from '@/router'

let DB = new VueDB();
let cancel, loadingInstance , promiseArr = {};
const CancelToken = axios.CancelToken;

//设置默认请求地址
/*if(process.env.NODE_ENV === "production"){  
  axios.defaults.baseURL = '';
}else {
  axios.defaults.baseURL = ''
}*/

// axios.defaults.baseURL = 'http://192.168.0.200:9010';
axios.defaults.baseURL = 'https://server1.buoyuu.com/supervise';
// axios.defaults.baseURL = 'https://cmis.buoyuu.com/bbmanager';

//设置默认请求头
axios.defaults.headers = {
  // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
  'Content-Type':"application/json",
  // 'access_token': '1'
};
//设置请求超时时间
axios.defaults.timeout = 10000;
axios.defaults.withCredentials = true;


//请求拦截器---即异常处理
axios.interceptors.request.use(config => {
  loadingInstance = Spin.setDefaultIndicator({
    indicator: {
      render() {
        return <i class="anticon anticon-loading anticon-spin ant-spin-dot"></i>;
      },
    },
  });
  DB.getItem('token').toString() && (config.headers['token'] = DB.getItem('token').toString());
  return config
}, error => {
  // loadingInstance.close();  // 关闭加载遮罩层
  warnMsgDialog('请求超时!');
  return Promise.reject(error)
});

//响应拦截器---即异常处理
axios.interceptors.response.use(response => {
  // loadingInstance.close();  // 关闭加载遮罩层
  return response
}, error => {
  if (error && error.response) {
    switch (error.response.status) {
      case 400:
        error.message = '错误请求'
        break;
      case 401:
        error.message = '未授权，请重新登录';
        Router.replace('/login')
        break;
      case 403:
        error.message = '拒绝访问'
        break;
      case 404:
        error.message = '请求错误,未找到该资源'
        break;
      case 405:
        error.message = '请求方法未允许'
        break;
      case 408:
        error.message = '请求超时'
        break;
      case 415:
        error.message = '请检查请求传参'
        break;
      case 500:
        error.message = '服务器端出错'
        break;
      case 501:
        error.message = '网络未实现'
        break;
      case 502:
        error.message = '网络错误'
        break;
      case 503:
        error.message = '服务不可用'
        break;
      case 504:
        error.message = '网络超时'
        break;
      case 505:
        error.message = 'http版本不支持该请求'
        break;
      default:
        error.message = `连接错误${error.response.status}`
    }
  } else {
    error.message = "连接到服务器失败"
  }
  // loadingInstance.close(); // 关闭加载遮罩层
  warnMsgDialog(error.message)
  return Promise.resolve(error.response)
})

export default {
  //get
  get (url,param) {
    return new Promise((resolve,reject) => {
      axios({
        method: 'get',
        url,
        params: param, // qs.stringify(param) formData传参方式
        cancelToken: new CancelToken(c => {
          cancel = c
        })
      }).then(res => {
        console.log(res)
        res.data && (res.data.code === 0 ? resolve(res.data) : errorMsgDialog(res.data.message));
      }).catch(res => {
        console.log(res)
        res.data && (res.data.code === 0 ? reject(res.data) : errorMsgDialog(res.data.message));
      })
    })
  },
  //post
  postFormData (url,param) {
    return new Promise((resolve,reject) => {
      axios({
        method: 'post',
        url,
        data: qs.stringify(param), // qs.stringify(param)formData传参方式
        cancelToken: new CancelToken(c => {
          cancel = c
        })
      }).then(res => {
        console.log(res)
        res.data && (res.data.code === 0 ? resolve(res.data) : errorMsgDialog(res.data.message));
      }).catch(res => {
        res.data && (res.data.code === 0 ? reject(res.data) : errorMsgDialog(res.data.message));
      })
    })
  },
  //post
  post (url,param) {
    return new Promise((resolve,reject) => {
      axios({
        method: 'post',
        url,
        data: param, // qs.stringify(param)formData传参方式
        cancelToken: new CancelToken(c => {
          cancel = c
        })
      }).then(res => {
        console.log(res)
        res.data && (res.data.code === 0 ? resolve(res.data) : errorMsgDialog(res.data.message));
      }).catch(res => {
        res.data && (res.data.code === 0 ? reject(res.data) : errorMsgDialog(res.data.message));
      })
    })
  },
  //getFile
  getFile (url,param) {
    return new Promise((resolve,reject) => {
      axios({
        method: 'get',
        url,
        data: param, // qs.stringify(param)
        cancelToken: new CancelToken(c => {
          cancel = c
        }),
        responseType: 'arraybuffer'
      }).then(res => {
        resolve(res)
      }).catch(res => {
        reject(res)
      })
    })
  }
}
